package com.example.administrator.himalaya.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.callback.GuideService;
import com.example.administrator.himalaya.entity.categoryentity.AdvertiseEntity;
import com.example.administrator.himalaya.entity.categoryentity.GuideEntity;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class GuideActivity extends AppCompatActivity implements Callback<GuideEntity>, View.OnClickListener {

    private ImageView imageView;
    private List<AdvertiseEntity> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        imageView = (ImageView) findViewById(R.id.iv_guide_activity);
     Download();
    }
    private void Download() {
        GuideService service = new Retrofit.Builder()
                .baseUrl("http://adse.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(GuideService.class);
        service.datas(0,"android","loading","wifi",0,"4.3.98").enqueue(this);

        init();
    }
    private void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent intent =new Intent(GuideActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        }).start();

    }


    @Override
    public void onResponse(Call<GuideEntity> call, Response<GuideEntity> response) {
        data = response.body().getData();
        Picasso.with(this).load(data.get(0).getCover()).into(imageView);
        imageView.setOnClickListener(this);
    }
    @Override
    public void onFailure(Call<GuideEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        Intent intent =new Intent(GuideActivity.this,CitroenActivity.class);
        String link = data.get(0).getLink();
        intent.putExtra("link",link);
        startActivity(intent);
    }
}
